<?php

namespace app\admin\controller;

use think\facade\Request;
use think\Validate;
use app\common\model\UserModel;
use app\common\model\SigninModel;
use app\common\model\SigninHistoryModel;

class SigninController extends BaseController
{

    public function index()
    {
        return $this->fetch();
    }

    public function index_handle()
    {
        $where = [];
        if ($rank = Request::param('rank') != '') {
            $where[] = ['rank', '=', $rank];
        }
        if ($name = Request::param('name') != '') {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        $list = SigninModel::where($where)->order("stage", "desc")->select();

        if (count($list) > 0) {
            return json(["code" => 0, "msg" => "", "data" => $list, "count" => count($list)]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add()
    {
        return $this->fetch();
    }

    public function add_handle()
    {
        $params = Request::param();
        $validate    = new Validate([
            'name'  => 'require|length:1,50',
            'quantity'  => 'require',
            'thumb'  => 'require|length:1,500',
            'stage'  => 'require|length:1,100',
            'type'  => 'require|number|in:1,2,3,4,5,6,7,105',
            'rank'  => 'require|number|in:1,2,3,4,5',
        ], [
            "name"  => "物品名称在1-50个字符",
            'quantity'  => '请输入物品奖励数量',
            "thumb"  => "请正确上传奖品缩略图",
            "stage" => "请填写奖励关卡",
            "type" => "请选择奖励物品类型",
            "rank" => "请填写物品等级(1-5)",
        ]);

        if (!$validate->check($params)) {
            return self::bulidFail($validate->getError());
        }

        if (SigninModel::where("stage", $params['stage'])->count() >= 1) {
            return self::bulidFail("已存在的奖励关卡: " . $params['stage']);
        }

        $new = new SigninModel($params);

        if ($new->save()) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function edit()
    {
        $id = Request::param("id");
        $ret = SigninModel::get($id);
        $this->assign("ret", $ret);
        return $this->fetch();
    }

    public function edit_handle()
    {
        $id = Request::param("id");
        if ($id <= 0) {
            return self::bulidFail();
        }
        $params = Request::param();
        $validate    = new Validate([
            'name'  => 'require|length:1,50',
            'quantity'  => 'require',
            'thumb'  => 'require|length:1,500',
            'stage'  => 'require|length:1,100',
            'type'  => 'require|number|in:1,2,3,4,5,6,7,105',
            'rank'  => 'require|number|in:1,2,3,4,5',
        ], [
            "name"  => "物品名称在1-50个字符",
            'quantity'  => '请输入物品奖励数量',
            "thumb"  => "请正确上传奖品缩略图",
            "stage" => "请填写奖励关卡",
            "type" => "请选择奖励物品类型",
            "rank" => "请填写物品等级(1-5)",
        ]);

        if (!$validate->check($params)) {
            return self::bulidFail($validate->getError());
        }

        $ret = SigninModel::get($id);
        $ret->name = $params['name'];
        $ret->quantity = $params['quantity'];
        $ret->thumb = $params['thumb'];
        $ret->stage = $params['stage'];
        $ret->type = $params['type'];
        $ret->rank = $params['rank'];

        if ($ret->save()) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function delete()
    {
        $id = Request::param("id");
        $ret = SigninModel::where("id", $id)->delete();
        if ($ret) {
            return self::bulidSuccess();
        }
        return self::bulidFail();
    }

    public function history()
    {
        return $this->fetch();
    }

    public function history_handle()
    {
        $where = [];
        $name = Request::param('name');
        $nickname = Request::param('nickname');
        $rank = Request::param('rank');
        $user_id = Request::param("user_id");

        if ($name != '') {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        if ($nickname != '') {
            $where[] = ['nickname', 'like', "%{$nickname}%"];
        }
        if ($rank != '') {
            $where[] = ['rank', '=', $rank];
        }
        if($user_id) {
            $where[] = ['user_id', "=", $user_id];
        }

        $count = SigninHistoryModel::where($where)->count();

        $list = SigninHistoryModel::where($where)->page($this->get('page', 1), $this->get('limit', 20))->order("id", "desc")->select();

        if (count($list) > 0) {
            return json(["code" => 0, "msg" => "", "data" => $list, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }
}
